﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using System.Windows.Forms;
using MyDBconnection;
using Cat;
using System.Security.Cryptography;


namespace Thesis_2
{
    public partial class frmStock : Form
    {
        ConnectionString cs = new ConnectionString();
        OdbcCommand cmd = null;
        OdbcConnection con = null;
        OdbcDataReader rdr = null;
        DatabaseConnection dbconn = new DatabaseConnection();
        DataTable tbl = new DataTable();
        int i = 0, Index;
        
        float qtyValue, qtyAmount, Total = 0;
        public frmStock()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmStock_Load);
            txtQuantity.KeyPress += new KeyPressEventHandler(txtQuantity_KeyPress);
            txtsearch.TextChanged += new EventHandler(txtsearch_TextChanged);
            btnInstock.Click += new EventHandler(btnInstock_Click);
            btnClear.Click += new EventHandler(btnClear_Click);

            txtprod.TextChanged += new EventHandler(txtprod_TextChanged);
            dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_RowHeaderMouseClick);
            tabControl1.Click += new EventHandler(tabControl1_Click);
            //Update
            txtQuantityU.KeyPress += new KeyPressEventHandler(txtQuantityU_KeyPress);
            txtSearchName.TextChanged += new EventHandler(txtSearchName_TextChanged);
            txtSearchID.TextChanged += new EventHandler(txtSearchID_TextChanged);
            dataGridView2.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView2_RowHeaderMouseClick);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
        }
        void stockupdate()
        {
            
                qtyValue = (float)txtRemainingQTY.Value;
                qtyAmount = (float)txtQuantityU.Value;
                Total = qtyAmount + qtyValue;
                txtStockupdate.Text = Total.ToString();

            

            
            
        }
        void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtproductIDU.Text == "")
            {
                MessageBox.Show("Please Search Product ID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSearchName.Focus();
                return;
            }
            if (txtQuantityU.Text == "")
            {
                MessageBox.Show("Please Enter Quantity", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtQuantityU.Focus();
                return;
            }
            stockupdate();
            dbconn.dbconnection();
            dbconn.UpdateData("Update tblstock set Quantity='"+ txtStockupdate.Text + "'Where ProductID='" + txtproductIDU.Text + "';");
            MessageBox.Show("Successfuly Updated", "Stock Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
            dbconn.DBClose();
            GetStock();
            txtQuantityU.Text = "";
            txtproductIDU.Text = "";
            txtRemainingQTY.Text = "";
            txtStockupdate.Text = "";
        }
        void tabControl1_Click(object sender, EventArgs e)
        {
            Getproduct();
            GetStock();
        }
        void txtSearchID_TextChanged(object sender, EventArgs e)
        {
            if (txtSearchID.Text != "SEARCH PRODUCT ID")
            {
                dbconn.dbconnection();
                dbconn.loadData("SELECT * FROM tblstock WHERE  ProductID  LIKE '%" + txtSearchID.Text + "%'");
                dataGridView2.DataSource = dbconn.tbl;
                dbconn.DBClose();
            }
            else
            {
                GetStock();
            }
        }
        void txtSearchName_TextChanged(object sender, EventArgs e)
        {
            if (txtSearchName.Text != "SEARCH SUPPLIER NAME")
            {
                dbconn.dbconnection();
                dbconn.loadData("SELECT * FROM tblstock WHERE  Supplier LIKE '%" + txtSearchName + "%'");
                dataGridView2.DataSource = dbconn.tbl;
                dbconn.DBClose();
            }
            else
            {
                GetStock();
            }
        }
        void dataGridView2_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow dr = dataGridView2.SelectedRows[0];
            txtproductIDU.Text = dr.Cells[1].Value.ToString();
            txtRemainingQTY.Text = dr.Cells[3].Value.ToString();

        }
        void txtQuantityU_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        // Tab1
        void btnClear_Click(object sender, EventArgs e)
        {
            reset();
        }
        void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow dr = dataGridView1.SelectedRows[0];
            txtProduct.Text = dr.Cells[0].Value.ToString();
            txtSupplier.Text = dr.Cells[3].Value.ToString();
        }
        void txtprod_TextChanged(object sender, EventArgs e)
        {
            if (txtsearch.Text != "Search Product")
            {
                dbconn.dbconnection();
                dbconn.loadData("SELECT * FROM tblproduct WHERE  ProductID LIKE '%" + txtsearch.Text + "%'");
                dataGridView1.DataSource = dbconn.tbl;
                dbconn.DBClose();
            }
            else
            {
                Getproduct();
            }
        }
        void txtsearch_TextChanged(object sender, EventArgs e)
        {
            if (txtsearch.Text != "Search Product")
            {
                dbconn.dbconnection();
                dbconn.loadData("SELECT * FROM tblproduct WHERE  Productname LIKE '%" + txtsearch.Text + "%'");
                dataGridView1.DataSource = dbconn.tbl;
                dbconn.DBClose();
            }
            else
            {
                Getproduct();
            }
        }
        //Tab1 btn
        void btnInstock_Click(object sender, EventArgs e)
        {
            if (txtProduct.Text == "")
            {
                MessageBox.Show("Please Search Product ID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtsearch.Focus();
                return;
            }
            if (txtQuantity.Text == "")
            {
                MessageBox.Show("Please Enter Quantity of Product", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtQuantity.Focus();
                return;
            }
            if (txtSupplier.Text == "")
            {
                MessageBox.Show("Please Enter Supplier", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSupplier.Focus();
                return;
            }
            try
            {
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct = "Select ProductID from tblstock where ProductID ='" + txtProduct.Text + "'";
                cmd = new OdbcCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();
                if (rdr.Read())
                {
                    MessageBox.Show("Product ID Already in the Stock", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtProduct.Text = "";
                    txtQuantity.Text = "";
                    txtSupplier.Text = "";
                    if ((rdr != null))
                    {
                        rdr.Close();
                    }
                    return;
                }
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string cb = "Insert into tblstock(StockID,ProductID,Supplier,Quantity) VALUES ('"+txtStockID.Text+"','" + txtProduct.Text + "','" + txtSupplier.Text + "','" + txtQuantity.Text + "')";
                cmd = new OdbcCommand(cb);
                cmd.Connection = con;
                cmd.ExecuteReader();
                con.Close();
                MessageBox.Show("Successfully added", "Stock In", MessageBoxButtons.OK, MessageBoxIcon.Information);
                reset();       
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        void reset()
        {
            txtProduct.Text = "";
            txtSupplier.Text = "";
            txtQuantity.Text = "";
            txtsearch.Focus();
        }
        void txtQuantity_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        void frmStock_Load(object sender, EventArgs e)
        {
            Getproduct();
            GetStock();
            autoid();
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridView2.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }
        void Getproduct()
        {
            dbconn.dbconnection();
            dbconn.loadData("Select * from tblproduct");
            dataGridView1.DataSource = dbconn.tbl;
            dbconn.DBClose();
            dataGridView1.Columns[5].DefaultCellStyle.Format = "#,#0.00";
            dataGridView1.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        }
        void GetStock()
        {
            dbconn.dbconnection();
            dbconn.loadData("Select * from tblstock");
            dataGridView2.DataSource = dbconn.tbl;
            dbconn.DBClose();
            dataGridView2.Columns[3].DefaultCellStyle.Format = "#,#0.00";
            dataGridView2.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        
        }
        public static string GetUniqueKey(int maxSize)
        {
            char[] chars = new char[62];
            chars = "123456789".ToCharArray();
            byte[] data = new byte[1];
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);
            data = new byte[maxSize];
            crypto.GetNonZeroBytes(data);
            StringBuilder result = new StringBuilder(maxSize);
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return result.ToString();
        }
        public void autoid()
        {
            txtStockID.Text = "ST-ID-" + GetUniqueKey(5);
        }
    }
}
